package com.ibeeking.found.open.b.rest.service.mq;

import com.ibeeking.found.common.constants.WxCpServiceTypeConstant;
import com.ibeeking.found.common.entity.MqMsgEventParam;
import com.ibeeking.found.common.entity.open.OpenAuthDTO;
import com.ibeeking.found.common.entity.open.OpenAuthQuery;
import com.ibeeking.found.common.enums.OpenApiConfigEnum;
import com.ibeeking.found.common.enums.WxCpAppTypeEnum;
import com.ibeeking.found.cop.api.common.param.MsgEventDepartmentParam;
import com.ibeeking.gmc.global.api.feign.GlobalOpenFeignClient;
import com.ibeeking.found.open.b.rest.service.IMqService;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WxCpTpContactMsgEventHandler {

    @Resource
    private IMqService mqServiceImpl;

    @Resource
    private GlobalOpenFeignClient globalOpenFeignClient;

    public void processCreateDepartmentMsgEvent(WxCpTpXmlMessage wxCpTpXmlMessage){
        String suiteId = wxCpTpXmlMessage.getSuiteId();
        String authCorpId = wxCpTpXmlMessage.getAuthCorpId();

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentIdAndAppId();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpTpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpTpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpTpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpTpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpTpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpTpXmlMessage.getCreateTime());
        MsgEventDepartmentParam departmentParam = new MsgEventDepartmentParam();
        departmentParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        departmentParam.setId(StringUtils.isNotEmpty(wxCpTpXmlMessage.getId())?Long.valueOf(wxCpTpXmlMessage.getId()):null);
        departmentParam.setName(wxCpTpXmlMessage.getName());
        departmentParam.setNickName(wxCpTpXmlMessage.getAlias());
        departmentParam.setSort(wxCpTpXmlMessage.getOrder());
        departmentParam.setParentId(StringUtils.isNotEmpty(wxCpTpXmlMessage.getParentId())?Long.valueOf(wxCpTpXmlMessage.getParentId()):null);
        departmentParam.setAuthCorpAppId(wxCpTpXmlMessage.getAuthCorpId());
        msgEventParam.setParams(JsonUtils.toJsonStr(departmentParam));
        LogUtils.info(">>>>>>> CREATE_PARTY:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }

    private OpenAuthDTO queryWxOpenAuthListByComponentIdAndAppId() {
        OpenAuthQuery query = new OpenAuthQuery();
        query.setConfigType(OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode());
        query.setConfigSubType(WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode());
        query.setAppType(WxCpServiceTypeConstant.CP_TYPE_SB);
        ResponseResult<OpenAuthDTO> responseResult = globalOpenFeignClient.queryOpenAuthOne(query);
        return responseResult.getData();
    }

}
